// pages/orderTruck/listv2.js
import requestUtil from '../../baseRequest.js'
import requestUrls from '../../requestUrl.js'
import {
  $Message
} from '../../components/base/index'
import commonUtils from '../../utils/util.js'

let openid = wx.getStorageSync('openid')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openId:openid,
    currentTab:'FB',
    sonTab:'',
    orderCenterList:[],
    truckCenterList:[],
    pagination: {
      pageSize: 5,
      pageNum: 1,
      total: 0
    },
    statusId:'20',

  },
  //切换第一行标题
  changeCurrentTab(event){
    const tab = event.currentTarget.dataset.tab;
    this.setData({
      currentTab:tab,
      statusId:tab==='FB'?'20':'30',
      sonTab:tab==='YD'?'SQZ':'',
      statusColor:tab==='YD'?'rgb(0,216,240)':'white',
      orderCenterList:[],
      truckCenterList:[],
      pagination: {
        pageSize: 5,
        pageNum: 1,
        total: 0
      },
    });
    this.getPage();
  },
  //切换第二行标题
  changeSonTab(event){
    const tab = event.currentTarget.dataset.tab;
    let statusId = '';
    if(tab==='SQZ'){
      statusId ='SQZ';
    }else if(tab==='DQR'){
      statusId ='DQR';
    }else if(tab==='DZH'){
      statusId ='35';
    }else if(tab==='YSZ'){
      statusId ='40';
    }
    this.setData({
      sonTab:tab,
      statusId:statusId,
      orderCenterList:[],
      truckCenterList:[],
      pagination: {
        pageSize: 5,
        pageNum: 1,
        total: 0
      },
    });
    this.getPage();
  },
  //查询要展示的数据
  getPage (){
    const formData = {
      openId:this.data.openId,
      criteria:{
        // publishSide : this.data.publishSide,
        statusId:this.data.statusId,
        searchType:'list'
      },
      pageDto:{
        ...this.data.pagination
      }
    };
    if(this.data.publishSide==='TRUCK_CENTER'){
      requestUtil.postRequest(requestUrls.truckCenter.getPage, formData, (res) => {
        const dataList = res.list;
        console.log(res);
        if (!res.hasError) {
            let truckList = [];
            for (let i = 0; i < dataList.length; i++) {
              truckList.push({
                ...dataList[i],
                freeBeginTime: commonUtils.formatDate(dataList[i].freeBeginTime, 'MM-dd hh'),
                freeEndTime: commonUtils.formatDate(dataList[i].freeEndTime, 'MM-dd hh'),
              })
            }
            this.setData({
              truckCenterList : this.data.truckCenterList.concat(truckList),
              pagination:{
                ...this.data.pagination,
                total:res.total
              }
            })
        }
      })
    }else if(this.data.publishSide==='ORDER_CENTER'){
      requestUtil.postRequest(requestUrls.orderCenter.getPage, formData, (res) => {
        const dataList = res.list;
        if (!res.hasError) {
            let orderList = [];
            for (let i = 0; i < dataList.length; i++) {
              orderList.push({
                ...dataList[i],
                loadingBeginTime:commonUtils.formatDate(dataList[i].loadingBeginTime,'MM-dd hh'),
                loadingEndTime:commonUtils.formatDate(dataList[i].loadingEndTime,'MM-dd hh'),
              })
            }
            this.setData({
              orderCenterList : this.data.orderCenterList.concat(orderList),
              pagination:{
                ...this.data.pagination,
                total:res.total
              }
            })
        }
      })
    }
    
  },
  showErrorMessage(msg) {
    $Message({
      content: msg,
      type: 'error'
    })
  },
  linkToDetail(event){
    const id = event.currentTarget.dataset.id;
    //进入方为货主
    if(this.data.publishSide==='ORDER_CENTER'){
      let orderCenter ={};
      this.data.orderCenterList.forEach(order=>{
        if(order.id===id){
          orderCenter = order;
        }
      })
      let url = '';
      if(this.data.statusId==='20'){
        url = '/pages/orderCenter/detail?orderCenterId=' + id + '&publishSide=ORDER_CENTER'
      }else{
        url = '/pages/truckCenter/detail?publishSide=ORDER_CENTER&otiId=' + orderCenter.truckOrderInfoId
      }

      // if(orderCenter.statusId==='30'){
      //   url = '/pages/truckCenter/detail?publishSide='+this.data.publishSide+"&orderCenterId="+id
      // }else{
      //   url = '/pages/orderCenter/detail?publishSide='+this.data.publishSide+"&orderCenterId="+id
      // }
      //跳转到委托单明细页面
      wx.navigateTo({
        url:url
      });
    }else if(this.data.publishSide==='TRUCK_CENTER'){
      let truckCenter ={};
      this.data.truckCenterList.forEach(order=>{
        if(order.id===id){
          truckCenter = order;
        }
      })
      console.log(truckCenter);
      let url = '';
      //状态为已发布
      if(this.data.statusId==='20'){
        url = '/pages/truckCenter/detail?publishSide=TRUCK_CENTER&truckCenterId=' + id
      }else{
        url = '/pages/orderCenter/detail?publishSide=TRUCK_CENTER&otiId=' + truckCenter.truckOrderInfoId
      }
      // if(truckCenter.statusId==='30'){
      //   url = '/pages/orderCenter/detail?publishSide='+this.data.publishSide+"&truckCenterId="+id
      // }else{
      //   url = '/pages/truckCenter/detail?publishSide='+this.data.publishSide+"&truckCenterId="+id
      // }
      //跳转到运输单详情页面
      wx.navigateTo({
        url:url
      });
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { publishSide } = options;
    this.setData({
      publishSide:publishSide||'ORDER_CENTER',
      orderCenterList:[],
      truckCenterList:[],
    });
    this.getPage();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const {pageSize,pageNum,total} = this.data.pagination;
    if(pageSize*pageNum>=total){
      this.showErrorMessage("已加载全部数据");
    }else{
      this.setData({
        pagination:{
          ...this.data.pagination,
          pageNum:pageNum+1
        }
      });
      this.getPage();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})